package com.dayatang.android.Activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.dayatang.android.Db.SystemMessageServiceDb;
import com.dayatang.android.Model.SystemMessage;
import com.dayatang.android.Adapter.SystemMessageListViewAdapter;

public class SystemMessageActivity extends FootCommonActivity {
	
	protected static final int CONTEXT_ITEMS_SIGN_READ = 0;
	protected static final int CONTEXT_ITEMS_DEL_THIS = 1;
	
	private ListView smListView;
	private SystemMessageServiceDb systemMessageServiceDb;
	private RadioGroup radioGroup;
	private RadioButton allRadioButton;
	private TextView infoTextView;
	
	private String itemId;
	private String signRead;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.systemMessageServiceDb = new SystemMessageServiceDb(this);
		AlabSetBarTitleText(getString(R.string.index_system_message));
		AlabSetContentView(R.layout.system_message_activity);
		InitListView();
		InitRadioButton();
	}
	
	private void InitRadioButton() {
		radioGroup = (RadioGroup) findViewById(R.id.sm_radioGroup);
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				int radioButtonId = group.getCheckedRadioButtonId();
				if (radioButtonId == R.id.sm_unread_Rad) {
					ClickRad("already_read=?", new String[]{"0"});
				} else if (radioButtonId == R.id.sm_all_Rad) {
					ClickRad(null,null);
				}
			}
		});
	}
	
	private void ClickRad(String selection , String[] selectionArgs) {
		InitData(selection,selectionArgs);
	}
	
	private void InitListView() {
		smListView = (ListView) findViewById(R.id.system_message_listView);
		allRadioButton = (RadioButton) findViewById(R.id.sm_all_Rad);
		InitData("already_read=?", new String[]{"0"});
	}
	
	private void InitData(String selection , String[] selectionArgs) {
		List<HashMap<String, Object>> maps = getDataBySelection(selection,selectionArgs);
		infoTextView = (TextView) findViewById(R.id.sm_infoTextview);
		if (maps.size() > 0) {
			infoTextView.setVisibility(View.INVISIBLE);
			smListView.setAdapter(setSMAdapter(maps));
			smListView.setOnCreateContextMenuListener(null);
			smListView.setOnItemLongClickListener(new OnItemLongClickListener() {
				public boolean onItemLongClick(AdapterView<?> parent,
						View view, int position, long id) {
					signRead = ((TextView)view.findViewById(R.id.sm_listview_item_sign_read)).getText().toString();
					itemId = ((TextView)view.findViewById(R.id.sm_listview_item_thisid)).getText().toString();
					return false;
				}
			});
			
			smListView.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){
				public void onCreateContextMenu(ContextMenu menu, View v,
						ContextMenuInfo menuInfo) {
					if (signRead.equals("0")) {
						menu.add(0, CONTEXT_ITEMS_SIGN_READ, 0, getString(R.string.sm_sign_read));
					}
					menu.add(0, CONTEXT_ITEMS_DEL_THIS, 1, getString(R.string.sm_del_this));
				}
			});
		} else {
			smListView.setAdapter(null);
			infoTextView.setVisibility(View.VISIBLE);
		}	
	}
	
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case CONTEXT_ITEMS_SIGN_READ:
				signRead();
				return true;
			case CONTEXT_ITEMS_DEL_THIS:
				delThis();
				return true;
		}
		return super.onContextItemSelected(item);
	}	
	
	private void signRead() {
		systemMessageServiceDb.updateBySelection(new String[]{itemId});
		allRadioButton = (RadioButton) findViewById(R.id.sm_all_Rad);
		if (allRadioButton.isChecked()) {
			ClickRad(null,null);
		} else {
			ClickRad("already_read=?", new String[]{"0"});
		}
	}
	
	private void delThis() {
		systemMessageServiceDb.delBySelection("thisid=?", new String[]{itemId});
		allRadioButton = (RadioButton) findViewById(R.id.sm_all_Rad);
		if (allRadioButton.isChecked()) {
			ClickRad(null,null);
		} else {
			ClickRad("already_read=?", new String[]{"0"});
		}
	}
	
	private List<HashMap<String, Object>> getDataBySelection(String selection , String[] selectionArgs) {
		List<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
		List<SystemMessage> systemMessages = systemMessageServiceDb.findSM(selection,selectionArgs);
		for (SystemMessage systemMessage : systemMessages) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("ItemTitle", systemMessage.getTitle());
			map.put("ItemContent", systemMessage.getContent());
			map.put("ItemOrganizationName", systemMessage.getOrganizationName());
			map.put("ItemReleaseDate", systemMessage.getReleaseDate());
			map.put("ItemthisId", systemMessage.getThisid());
			map.put("ItemSignRead", systemMessage.getAlreadyRead());
			listItem.add(map);
		}
		return listItem;
	}
	
	private SystemMessageListViewAdapter setSMAdapter(List<HashMap<String, Object>> maps) {
		return new SystemMessageListViewAdapter(this, maps);
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			this.finish();
			overridePendingTransition(R.anim.translucent_enter, R.anim.translucent_exit);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
